package com.gameguru.mathgame;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SubGame extends Activity implements Runnable {
	private Integer answer = -1;
	private int qNo = 0;
	private long playedTime = 0L;
	private long startTime = 0L;
	private int score = 0;
	int noOfQuestions = 0;
	Button button1, button2, button3, button4, button5, button6;
	Thread currentThread;
	boolean isWaiting = false;
	GameHelper gameHelper;

	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.add_layout);
		gameHelper = new GameHelper(this);
		this.init();
		this.resetImage();
		this.startTimer();
		this.startGame();
	}

	void init() {
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		button4 = (Button) findViewById(R.id.button4);
		button5 = (Button) findViewById(R.id.button5);
		button6 = (Button) findViewById(R.id.button6);

		if (noOfQuestions == 0) {
			noOfQuestions = gameHelper.getNoOfQuestions();
		}
		LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout2);
		layout = gameHelper.bgSettings(layout);
	}

	private void setCurrentQuestion() {
		TextView textView = (TextView) findViewById(R.id.textView2);
		String text = (qNo + 1) + " of " + noOfQuestions;
		textView.setText(text);

	}

	private void startTimer() {

		if (startTime == 0) {
			startTime = System.currentTimeMillis();
		} else {
			startTime = System.currentTimeMillis() - playedTime;
		}
	}

	private void pauseTimer() {
		long currentTime = System.currentTimeMillis();
		playedTime = currentTime - startTime;
	}

	private void startGame() {

		if (qNo < noOfQuestions) {
			setSum();
			constructAnswerList();
			setCurrentQuestion();
		} else {
			// Game over. show the result
			this.pauseTimer();
			this.showResult();
		}
		qNo++;
	}

	private void showResult() {
		Intent intent = new Intent().setAction(Constants.SHOW_RESULT_ACTION);
		intent.putExtra("score", score);
		intent.putExtra("time", playedTime);
		intent.putExtra("totalQuestions", noOfQuestions);
		intent.putExtra(DBHelper.TEST_TYPE, Constants.SUBTRACTION);
		startActivity(intent);
		this.finish();
	}

	private void displayAnswer(int i, String choice) {
		switch (i) {
		case 1:
			Button button1 = (Button) findViewById(R.id.button1);
			button1.setText(choice);
			break;
		case 2:
			Button button2 = (Button) findViewById(R.id.button2);
			button2.setText(choice);
			break;
		case 3:
			Button button3 = (Button) findViewById(R.id.button3);
			button3.setText(choice);
			break;
		case 4:
			Button button4 = (Button) findViewById(R.id.button4);
			button4.setText(choice);
			break;
		case 5:
			Button button5 = (Button) findViewById(R.id.button5);
			button5.setText(choice);
			break;
		case 6:
			Button button6 = (Button) findViewById(R.id.button6);
			button6.setText(choice);
			break;
		}

	}

	private void constructAnswerList() {
		String choice;
		Integer integer = 0;
		Integer correctAnswer;
		Random randomNumbers = new Random();
		boolean isAdded;
		HashSet<Integer> answerSet = new HashSet<Integer>();
		correctAnswer = randomNumbers.nextInt(Constants.FIVE);
		choice = correctAnswer.toString();
		displayAnswer(correctAnswer + 1, answer.toString());
		answerSet.add(answer);

		for (int i = 0; i <= Constants.NO_OF_ANSWERS;) {
			integer = randomNumbers.nextInt(14);
			integer = (answer < 10) ? integer : (integer + 5);
			if (integer >= 5) {
				choice = integer.toString();
				isAdded = false;
				if (i != correctAnswer + 1) {
					isAdded = answerSet.add(integer);
					if (isAdded) {
						displayAnswer(i, choice);
						i++;
					}
				} else {
					i++;
				}
			}

		}

	}

	private void setSum() {
		ArrayList<Integer> randomNoList = genNumbers(2);
		if (randomNoList != null) {
			TextView textview = (TextView) findViewById(R.id.sum);
			String sum = randomNoList.get(0).toString() + "   -   "
					+ randomNoList.get(1).toString() + "   =   ?";
			textview.setText(sum);

		}
	}

	private ArrayList<Integer> genNumbers(Integer noOfNumbers) {
		ArrayList<Integer> randomNoList = new ArrayList<Integer>();
		Random randomNumbers = new Random();
		int i = 0;
		answer = 0;
		while (i < noOfNumbers) {
			Integer integer = randomNumbers.nextInt(Constants.TWENTY);
			if (integer >= 6 && randomNoList.size() < 1) {
				randomNoList.add(integer);
				i++;
				answer = integer;
			} else if (integer >= 2 && randomNoList.size() >= 1
					&& randomNoList.get(0) > integer) {
				randomNoList.add(integer);
				i++;
				answer = answer - integer;
			}
		}
		return randomNoList;
	}

	public void onButtonClick(View view) {
		if (!isWaiting) {
			isWaiting = true;
			Button button = (Button) findViewById(view.getId());
			String ansString = button.getText().toString();
			if (ansString.equals(answer.toString())) {
				score = score + 1;
				button.setBackgroundResource(R.drawable.maru);
			} else {
				button.setBackgroundResource(R.drawable.batsu);
			}
			Thread thread = new Thread(this);
			thread.start();
		}
	}

	public void resetImage() {
		button1.setBackgroundResource(R.drawable.grey);
		button2.setBackgroundResource(R.drawable.grey);
		button3.setBackgroundResource(R.drawable.grey);
		button4.setBackgroundResource(R.drawable.grey);
		button5.setBackgroundResource(R.drawable.grey);
		button6.setBackgroundResource(R.drawable.grey);
	}

	@Override
	public void run() {
		try {
			Thread.sleep(500);
			Message msg = new Message();
			handler.sendMessage(msg);
			isWaiting = false;

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			try {
				resetImage();
				startGame();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	};

}
